extends Node2D

@onready var player: Ark_Player = $Player
@onready var npc: Npc = $Npc
@onready var pointer_mark: PointerMark = $PointerMark

const AUTO_MOVE_NONE = 0
const AUTO_MOVE = 1
const AUTO_MOVE_FIN = 2
const AUTO_MOVE_SPEED: float = 50
var auto_move_state = AUTO_MOVE_NONE

@onready var fade_text: Label = $FadeText
const FADE_NONE : int = 0
const FADE_IN : int = 1
const FADE_IN_FIN : int = 2
const FADE_OUT : int = -1
const FADE_OUT_FIN : int = -2
const FADE_IN_TIME : float = 1.0
const FADE_OUT_TIME : float = 1.0
var fade_state : int = FADE_NONE
var elapsed : float = 0
var space_pressed : bool = false 
const SPAWN_POINT: Vector2 = Vector2(8, 90)

func start_fade_in():
	fade_state = FADE_IN

func start_fade_out():
	fade_state = FADE_OUT
	fade_text.visible = false

# option
@onready var option1 = $Option1
@onready var option2 = $Option2

func show_options():
	option1.visible = true
	option2.visible = true

func hide_options():
	option1.visible = false
	option2.visible = false	
	
# bubble
@onready var bubble_upper := $BubbleUpper
@onready var bubble_lower := $BubbleLower

@onready var falling_bubble_1 := $FallingBubble1
@onready var falling_bubble_2 := $FallingBubble2
@onready var falling_bubble_3 := $FallingBubble3
@onready var falling_bubble_4 := $FallingBubble4

@onready var bubble_text_upper = $BubbleUpper/Label
@onready var bubble_text_lower = $BubbleLower/Label

const LOWER_BUBBLE_ENTER_NONE : int = 0
const LOWER_BUBBLE_ENTER : int = 1
const LOWER_BUBBLE_ENTER_FIN: int = 2
const LOWER_BUBBLE_ENTER_DELAY : float = 1.5
var lower_bubble_enter_state: int = LOWER_BUBBLE_ENTER_NONE
var direction: int = 0
var cur_opt: int = 0

func select(n: int) -> void:
	if n == 1:
		option1.frame = 1
		option2.frame = 0
	elif n == 2:
		option1.frame = 0
		option2.frame = 1

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	player.controllable = false
	auto_move_state = AUTO_MOVE
	player.velocity.x = AUTO_MOVE_SPEED
	player.position = SPAWN_POINT
	
	npc.flip_h = true
	
	hide_options()
	select(1)
	pointer_mark.player_stopped.connect(start_fade_in)
	fade_text.modulate.a = 0
	
	bubble_text_lower.visible = false
	bubble_text_upper.visible = false

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if auto_move_state == AUTO_MOVE and player.position.x > 45:
		player.velocity.x = 0
		player.controllable = true
		auto_move_state = AUTO_MOVE_FIN
	
	space_pressed = Input.is_action_just_pressed("space")
	
	if fade_state == FADE_IN:
		if space_pressed:
			fade_text.modulate.a = 1
			fade_state = FADE_IN_FIN
			show_options()
			return
		else:
			elapsed += delta
			fade_text.modulate.a = min(1, elapsed) / FADE_IN_TIME
			
			if fade_text.modulate.a >= 1:
				fade_state = FADE_IN_FIN
				show_options()
			
			return
	
	if fade_state == FADE_IN_FIN: # 动画结束才能做选择
		if Input.is_action_just_pressed("ui_left"):
			select(1)
		elif Input.is_action_just_pressed("ui_right"):
			select(2)
	
	if space_pressed:  # 确认选项
		bubble_upper.enabled = true
		bubble_text_upper.visible = true
		lower_bubble_enter_state = LOWER_BUBBLE_ENTER
		elapsed = 0
		start_fade_out()
		hide_options()
	
	if lower_bubble_enter_state == LOWER_BUBBLE_ENTER:
		elapsed += delta
		if elapsed >= LOWER_BUBBLE_ENTER_DELAY:
			bubble_lower.enabled = true
			bubble_text_lower.visible = true
			lower_bubble_enter_state = LOWER_BUBBLE_ENTER_FIN
			player.controllable = true
	
	if player.position.y >= 257:
		player.position = SPAWN_POINT
	
	if player.position.x >= 416:
		get_tree().change_scene_to_file("res://karte/karte_3.tscn")
